﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HtmlDoc = HtmlAgilityPack.HtmlDocument;
using WebFunctions;
using Newtonsoft.Json;

namespace AutoIndustryFiles
{
    class mitElectricScraper : FileLinksScraper2
    {
        public mitElectricScraper()
        {
            this.MainPage = "http://cn.mitsubishielectric.com";
            this.CompanyName = "三菱自动化";
            this.CompanyUrl = "http://cn.mitsubishielectric.com/fa/zh/download/dwn_idx_certification.asp";
            this.ColumnName = "首页 -> 技术服务 -> 资料下载";
            //this.ReviseXPath += new OnReviseXPath(FuJiScraper_ReviseXPath);            
        }

        public void GetCertLinks()
        {
            bool f = false;
            List<string> certs = new List<string> { "CCC", "CE", "LR", "GL", "BV", "DNV", "ABS", "CCS", "NK", "KR", "RINA", "TUV", "KEMA" };

            string url = "http://cn.mitsubishielectric.com/fa/zh/download/dwn_idx_certification.asp";
            UpdateMsg("mitElectricScraper", url, ref f);

            HtmlDoc doc = new HtmlDoc();

            doc.LoadHtml(WebFuncs.OpenUrlEx(url, "UTF-8"));

            //认证类别
            HtmlAgilityPack.HtmlNodeCollection hnc = doc.DocumentNode.SelectNodes(@"//select[@name=""pdtCategory""]/option[@value!=""""]");

            foreach (HtmlAgilityPack.HtmlNode node in hnc)
            {
                string catalog1 = node.NextSibling.InnerText;//一级类别名称
                string id1 = node.GetAttributeValue("value", ""); //一级类别id

                string url2 = "http://cn.mitsubishielectric.com/fa/zh/download/process_ajax_certification.asp?actions=getBigCategoryId&PdtCategory={0}";
                UpdateMsg("mitElectricScraper", url2, ref f);

                HtmlDoc doc2 = new HtmlDoc();
                doc2.LoadHtml(WebFuncs.OpenUrlEx(string.Format(url2, id1), "UTF-8"));

                HtmlAgilityPack.HtmlNodeCollection hnc2 = doc2.DocumentNode.SelectNodes(@"//option[@value!=""""]");

                if (hnc2 == null)
                    continue;

                foreach (HtmlAgilityPack.HtmlNode node2 in hnc2)
                {
                    string catalog2 = node2.NextSibling.InnerText;//二级类别名称
                    string id2 = node2.GetAttributeValue("value", ""); //二级类别id

                    string url_file = "http://cn.mitsubishielectric.com/fa/zh/download/process_ajax_certification.asp?actions=getCertDetail&ProCategory={0}";
                    UpdateMsg("mitElectricScraper", url_file, ref f);

                    HtmlDoc doc_file = new HtmlDoc();
                    doc_file.LoadHtml(WebFuncs.OpenUrlEx(string.Format(url_file, id2), "UTF-8"));

                    HtmlAgilityPack.HtmlNodeCollection hnc_file = doc_file.DocumentNode.SelectNodes(@"//table/tbody/tr");

                    if (hnc_file == null)
                        continue;
                    foreach (HtmlAgilityPack.HtmlNode node_file in hnc_file)
                    {
                        string th = node_file.SelectSingleNode("th").InnerText;

                        HtmlAgilityPack.HtmlNodeCollection hnc_subfile = node_file.SelectNodes("td");
                        if (hnc_subfile == null)
                            continue;

                        int i = 0;
                        foreach (HtmlAgilityPack.HtmlNode node_subfile in hnc_subfile)
                        {
                            HtmlAgilityPack.HtmlNode a_node = node_subfile.SelectSingleNode("a[@onclick]");
                            if (a_node != null)
                            {
                                FileLink lnk = new FileLink();
                                lnk.Id = FileLinks.Count.ToString();
                                lnk.Name = th;
                                lnk.Url = a_node.GetAttributeValue("onclick", "");
                                lnk.CategoryPath = catalog1 + "->" + catalog2;
                                lnk.FileType = certs[i] + "认证";
                                lnk.IsFileLink = true;
                                FileLinks.Add(lnk);
                            }
                            i++;
                        }
                    }
                }
            }
        }

        public void GetCatalogLinks()
        {
            bool f = false;
            string url = "http://cn.mitsubishielectric.com/fa/zh/download/dwn_idx_catalog.asp";
            UpdateMsg("mitElectricScraper", url, ref f);

            HtmlDoc doc = new HtmlDoc();

            doc.LoadHtml(WebFuncs.OpenUrlEx(url, "UTF-8"));

            //认证类别
            HtmlAgilityPack.HtmlNodeCollection hnc = doc.DocumentNode.SelectNodes(@"//select[@name=""catalogCategory""]/option[@value!=""""]");

            foreach (HtmlAgilityPack.HtmlNode node in hnc)
            {
                string catalog1 = node.NextSibling.InnerText;//一级类别名称
                string id1 = node.GetAttributeValue("value", ""); //一级类别id

                string url2 = "http://cn.mitsubishielectric.com/fa/zh/download/process_ajax_catalog.asp?actions=getBigCategoryId&catalogCategory={0}";
                UpdateMsg("mitElectricScraper", url2, ref f);

                HtmlDoc doc2 = new HtmlDoc();
                doc2.LoadHtml(WebFuncs.OpenUrlEx(string.Format(url2, id1), "UTF-8"));

                HtmlAgilityPack.HtmlNodeCollection hnc2 = doc2.DocumentNode.SelectNodes(@"//option[@value!=""""]");

                if (hnc2 == null)
                    continue;

                foreach (HtmlAgilityPack.HtmlNode node2 in hnc2)
                {
                    string catalog2 = node2.NextSibling.InnerText;//二级类别名称
                    string id2 = node2.GetAttributeValue("value", ""); //二级类别id

                    string url_file = "http://cn.mitsubishielectric.com/fa/zh/download/dwn_idx_catalogSearchData.asp?page=&catalogCategory=1&catalogKeywords=&BigCategory={0}";

                    HtmlDoc doc_file = new HtmlDoc();
                    doc_file.LoadHtml(WebFuncs.OpenUrlEx(string.Format(url_file, id2), "UTF-8"));

                    HtmlAgilityPack.HtmlNodeCollection hnc_file1 = doc_file.DocumentNode.SelectNodes(@"//a[@onclick]");
                    HtmlAgilityPack.HtmlNodeCollection hnc_file2 = doc_file.DocumentNode.SelectNodes(@"//table//table/tr[1]/td");

                    if (hnc_file1 == null || hnc_file2 == null)
                        continue;

                    for (int i = 0; i < hnc_file1.Count; i++)
                    {
                        FileLink lnk = new FileLink();
                        lnk.Id = FileLinks.Count.ToString();
                        lnk.Name = hnc_file2[i].InnerText;
                        lnk.Url = hnc_file1[i].GetAttributeValue("onclick", "");
                        lnk.CategoryPath = catalog1 + "->" + catalog2;
                        lnk.FileType = "样本";
                        lnk.IsFileLink = true;
                        FileLinks.Add(lnk);
                    }
                }
            }
        }

        public void GetManualLinks()
        {
            List<string> certs = new List<string> { "CCC", "CE", "LR", "GL", "BV", "DNV", "ABS", "CCS", "NK", "KR", "RINA", "TUV", "KEMA" };

            string url = "http://cn.mitsubishielectric.com/fa/zh/download/dwn_idx_manual.asp";

            HtmlDoc doc = new HtmlDoc();

            doc.LoadHtml(WebFuncs.OpenUrlEx(url, "UTF-8"));

            //认证类别
            HtmlAgilityPack.HtmlNodeCollection hnc = doc.DocumentNode.SelectNodes(@"//select[@name=""pcate""]/option[@value>6]");

            foreach (HtmlAgilityPack.HtmlNode node in hnc)
            {
                string catalog2 = node.NextSibling.InnerText;//二级类别名称
                string id2 = node.GetAttributeValue("value", ""); //二级类别id

                string url2 = "http://cn.mitsubishielectric.com/fa/zh/download/process_ajax.asp?actions=getBigCategoryId&PdtCategory={0}";

                HtmlDoc doc2 = new HtmlDoc();
                doc2.LoadHtml(WebFuncs.OpenUrlEx(string.Format(url2, id2), "UTF-8"));

                HtmlAgilityPack.HtmlNodeCollection hnc2 = doc2.DocumentNode.SelectNodes(@"//option[@value!=""""]");

                if (hnc2 == null)
                    continue;

                foreach (HtmlAgilityPack.HtmlNode node2 in hnc2)
                {
                    string catalog3 = node.NextSibling.InnerText;//手册大类别名称
                    string id3 = node.GetAttributeValue("value", ""); //手册大类类别id

                    string url_file = "http://cn.mitsubishielectric.com/fa/zh/download/dwn_idx_manual.asp?pcate={0}&mlan=&pstatus=&BigCategory={1}&SmallCategory=&sinceYear=&sinceMonth=&tillYear=&tillMonth=&name=&keywords=&actions=searchManual";

                    HtmlDoc doc_file = new HtmlDoc();
                    doc_file.LoadHtml(WebFuncs.OpenUrlEx(string.Format(url_file, id2,id3), "UTF-8"));

                    HtmlAgilityPack.HtmlNodeCollection hnc_names = doc_file.DocumentNode.SelectNodes(@"//table[@class=""tbl_inch"" and @style]/tr[1]/td[1]");
                    HtmlAgilityPack.HtmlNodeCollection hnc_href = doc_file.DocumentNode.SelectNodes(@"");

                    //需要登录，待处理
                }
            }
        }

        public override void GenerateAllLinks(string param)
        {
            GetCertLinks();
            GetCatalogLinks();
        }
    }
}
